package com.lam.framework.bean.service;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lam.common.components.file.FileConstant;
import com.lam.common.components.file.FileDownloadUtil;
import com.lam.common.components.file.model.FileBusiTypeEnum;
import com.lam.common.components.file.model.FileDownloadInfo;
import com.lam.common.components.file.model.FileStatusEnum;
import com.lam.common.components.file.service.FileService;
import com.lam.common.context.TokenUtils;
import com.lam.common.utils.CollectionUtil;
import com.lam.common.utils.DateUtil;
import com.lam.common.utils.StringUtils;
import com.lam.common.utils.file.FileUtil;
import com.lam.framework.bean.entity.SysFile;
import com.lam.framework.bean.mapper.FileMapper;

/**
 * 文件记录Service业务层处理
 * 
 * @author admin
 * @date 2022-10-08
 */
@Service
public class FileServiceImpl implements FileService {

    @Autowired
    private FileMapper fileMapper;

    @Override
	public FileDownloadInfo getDownloadInfo(FileBusiTypeEnum busiType, String sign) {
    	
        SysFile sysFile = selectBySign(busiType, sign);
        if(sysFile == null) {
        	return null;
        }
		return new FileDownloadInfo(busiType, sysFile.getFilePath(), true);
	}

	@Override
	public FileDownloadInfo createDownloadInfo(FileBusiTypeEnum busiType, String path, int expireDay) {
		
		String sign = FileDownloadUtil.sign(path);
		SysFile sysFile = selectBySign(busiType, sign);
        if(sysFile != null) {
        	return new FileDownloadInfo(busiType, sysFile.getFilePath(), true);
        }
		
        sysFile = new SysFile();
    	sysFile.setFilePath(path);
    	sysFile.setSign(sign);
    	sysFile.setBusiType(busiType.getCode());
    	sysFile.setStatus(FileStatusEnum.AVAILABLE.getCode());
    	sysFile.setKeyMd5(FileDownloadUtil.getKeyMd5());
    	if(expireDay>0) {
    		sysFile.setExpireTime(DateUtil.getDurationDate(expireDay));
    	}
    	sysFile.setCreateBy(TokenUtils.getLoginUsername());
    	sysFile.setCreateTime(new Date());
    	fileMapper.insert(sysFile);
    	return new FileDownloadInfo(busiType, sysFile.getFilePath(), false);
	}

	@Override
	public boolean deleteBySign(FileBusiTypeEnum busiType, String sign) {
		SysFile sysFile = selectBySign(busiType, sign);
        if(sysFile == null) {
        	return false;
        }
		return FileUtil.deleteFile(sysFile.getFilePath());
	}

	@Override
	public boolean deleteByPath(FileBusiTypeEnum busiType, String path) {
		
		return deleteBySign(busiType, FileDownloadUtil.sign(path));
	}
	
	private SysFile selectBySign(FileBusiTypeEnum busiType, String sign) {
		QueryWrapper<SysFile> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("sign", sign);
        queryWrapper.eq("file_type", busiType.getCode());
        queryWrapper.eq("status", FileStatusEnum.AVAILABLE.getCode());
        return CollectionUtil.getFirst(fileMapper.selectList(queryWrapper));
	}

    @Override
	public void updateUseFlagByFileName(String fileName) {
    	if(StringUtils.isBlank(fileName)) {
    		return;
    	}
		QueryWrapper<SysFile> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("file_path", fileName);
		SysFile sysFile = new SysFile();
		sysFile.setUseFlag(FileConstant.USE_FLAG_YES);
		fileMapper.update(sysFile, queryWrapper);
	}

}
